home *** CD-ROM | disk | FTP | other *** search
- window.addEventListener('load', function(event) { WiseStampScriptLoader.init(event); }, false);
- window.addEventListener('unload', function(event) { WiseStampScriptLoader.uninit(event); }, false);
-
- var WiseStampScriptLoader =
- {
- _prefs: null,
-
- get prefs()
- {
- if (!this._prefs)
- {
- this._prefs = Components.classes["@mozilla.org/preferences-service;1"].
- getService(Components.interfaces.nsIPrefService);
- this._prefs = this._prefs.getBranch("extensions.wisestamp.");
- }
- return this._prefs;
- },
-
- scripts:
- {
- gmail:
- {
- file: "webmail/gmail.js",
- /* normal and google app hosted */
- regex: [/https?:\/\/mail\.google\.com\/(a\/)?.*/],
- useParent: false
- },
-
- yahooclassic:
- {
- file: "webmail/yahooclassic.js",
- regex: [/https?:\/\/.*mail\.yahoo\.com\/mc\/compose.*/],
- useParent: false
- },
-
- yahooclassic2:
- {
- file: "webmail/yahooclassic2.js",
- regex: [/https?:\/\/.*mail\.yahoo\.com\/mc\/welcome.*_pg=compose.*/],
- useParent: true
- },
-
- yahoo:
- {
- file: "webmail/yahoo.js",
- //regex: [/https?:\/\/.*mail\.yahoo\.com\/dc\/launch.*/],
- //regex: [/https?:\/\/.*mail\.yahoo\.com\/dc\/blank.*/],
- regex: [/https?:\/\/.*mail\.yahoo\.com.*/],
- useParent: false
- },
-
- aol:
- {
- file: "webmail/aol.js",
- regex: [/https?:\/\/webmail\.aol\.com\/.*\/aim\/.+\/s|Suite\.aspx/],
- useParent: false
- },
-
- hotmail:
- {
- file: "webmail/hotmail.js",
- regex: [/https?:\/\/.*mail\.live\.com\/mail\/.*/],
- useParent: false
- }
- },
-
- init: function(event)
- {
- var appcontent = window.document.getElementById("appcontent");
- if (appcontent && !appcontent.wisestamped)
- {
- appcontent.wisestamped = true;
- appcontent.addEventListener("DOMContentLoaded", this, false);
- }
- },
-
- uninit: function(event)
- {
- var appcontent = window.document.getElementById("appcontent");
- if (appcontent) appcontent.removeEventListener("DOMContentLoaded", this, false);
- },
-
- handleEvent: function(event)
- {
- WiseStampUtils.log("[ScriptLoader.js::handleEvent] event.type = " + event.type);
-
- if((event.type=="load" || event.type=="DOMContentLoaded") && WisestampOverlay.enabled)
- {
- var unsafeWin = event.target.defaultView;
- if (unsafeWin.wrappedJSObject)
- unsafeWin = unsafeWin.wrappedJSObject;
-
- var eventWin = unsafeWin;
-
- var unsafeLoc = new XPCNativeWrapper(unsafeWin, "location").location;
- var href = new XPCNativeWrapper(unsafeLoc, "href").href;
- var usingParent = false;
- if (href == "about:blank")
- {
- WiseStampUtils.log("[ScriptLoader.js::handleEvent] href == about:blank");
- usingParent = true;
- unsafeWin = event.target.defaultView.parent;
-
- if (unsafeWin.wrappedJSObject)
- unsafeWin = unsafeWin.wrappedJSObject;
-
- unsafeLoc = new XPCNativeWrapper(unsafeWin, "location").location;
- href = new XPCNativeWrapper(unsafeLoc, "href").href;
- }
-
- WiseStampUtils.log("[ScriptLoader.js::handleEvent] href = " + href);
-
- if (this.canLoad(href))
- {
- WiseStampUtils.log("[ScriptLoader.js::handleEvent] canLoad = true");
-
- for (var site in this.scripts)
- {
- var script = this.scripts[site];
- if (usingParent && script.useParent == false) // yahooclassic2 specific patch [Sasha]
- continue;
-
- var match = script.regex.some(function(regex) { return regex.test(href); }, this);
- if (match)
- {
- WiseStampUtils.log("[ScriptLoader.js::handleEvent] identified " + site + ", launching " + script.file + ", usingParent=" + usingParent);
-
- var wisestampsigjs=this.getUrlContents("chrome://wisestamp/content/webmail/wisestampsig.js");
- var sitejs = this.getUrlContents("chrome://wisestamp/content/" + script.file);
- var scriptjs = sitejs + wisestampsigjs;
- this.injectScript(site,scriptjs, href, unsafeWin, eventWin);
- } //else
- //WiseStampUtils.log("[WiseStampScriptLoader.js::handleEvent] No site identified.");
- }
- } else
- WiseStampUtils.log("[ScriptLoader.js::handleEvent] canLoad = false");
-
- }
- },
-
- injectScript: function(site,script, href, unsafeContentWin, eventWin)
- {
- WiseStampUtils.log("[ScriptLoader.js::injectScript] >>>");
-
- var sandbox;
- var safeWin = new XPCNativeWrapper(unsafeContentWin);
-
- sandbox = new Components.utils.Sandbox(safeWin);
- sandbox.window = safeWin;
- sandbox.document = sandbox.window.document;
- sandbox.unsafeWindow = unsafeContentWin;
- sandbox.eventWin = eventWin;
-
- // patch missing properties on xpcnw
- sandbox.XPathResult = Components.interfaces.nsIDOMXPathResult;
- sandbox.__proto__ = sandbox.window;
-
- sandbox.WiseStampLog = function(msg) { WiseStampUtils.log(msg); };
- sandbox.strip_tags = function(str, allowed_tags) { return WiseStampUtils.strip_tags(str, allowed_tags); };
-
- sandbox.WiseStampSigCreated = function()
- {
- WiseStampComm.writeLog(WiseStampComm.LOG_EVENT_SIG_INSERT,site);
- }
-
- sandbox.HasExtension = function(id)
- {
- var em = Components.classes["@mozilla.org/extensions/manager;1"].getService(Components.interfaces.nsIExtensionManager);
- var addon = em.getItemForID(id);
- return addon != null;
- }
-
- var prefs = Components.classes["@mozilla.org/preferences-service;1"].
- getService(Components.interfaces.nsIPrefService);
- prefs = prefs.getBranch("extensions.wisestamp.");
- sandbox.WiseStampPrefs = WiseStampPrefs;
- sandbox.GetValue = function(aPref) {
- return prefs.getCharPref(aPref);
- }
- sandbox.GetBoolValue = function(aPref) {
- return prefs.getBoolPref(aPref);
- }
- sandbox.ToDataURI = function(aURL) {
- return WiseStampScriptLoader.toDataURI(aURL);
- }
- sandbox.GetSignature = function GetSignature_name(aType) {
- return WisestampSignatureFactory.createSignature(null, aType);
- }
- try {
- WiseStampUtils.log("[ScriptLoader.js::injectScript] injecting...");
- //WiseStampUtils.log(script);
-
- Components.utils.evalInSandbox("(function(){" + script + "})()", sandbox);
- //WiseStampUtils.log("[ScriptLoader.js::injectScript] done injecting");
- } catch(e) {
- WiseStampUtils.log("[ScriptLoader.js::injectScript] exception caught - " + e);
- Components.utils.reportError(e);
- }
- },
-
- canLoad: function(url)
- {
- var scheme = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService).extractScheme(url);
- return ((scheme == "http" || scheme == "https" || scheme == "file") && !/hiddenWindow\.html$/.test(url));
- },
-
- getUrlContents: function(aUrl)
- {
- var ioService = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService);
- var scriptableStream = Components.classes["@mozilla.org/scriptableinputstream;1"].getService(Components.interfaces.nsIScriptableInputStream);
-
- var channel = ioService.newChannel(aUrl, null, null);
- var input = channel.open();
- scriptableStream.init(input);
- var str = scriptableStream.read(input.available());
- scriptableStream.close();
- input.close();
-
- return str;
- },
-
- toDataURI: function(aURL)
- {
- var ios = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService);
- var chan = ios.newChannel(aURL, null, null);
- var file = null;
- if (chan instanceof Components.interfaces.nsIFileChannel)
- {
- file = chan.file;
- }
-
- var contentType = Components.classes["@mozilla.org/mime;1"].getService(Components.interfaces.nsIMIMEService).getTypeFromFile(file);
- var inputStream = Components.classes["@mozilla.org/network/file-input-stream;1"].createInstance(Components.interfaces.nsIFileInputStream);
- inputStream.init(file, 0x01, 0600, 0);
- var stream = Components.classes["@mozilla.org/binaryinputstream;1"].createInstance(Components.interfaces.nsIBinaryInputStream);
- stream.setInputStream(inputStream);
- var encoded = btoa(stream.readBytes(stream.available()));
- return "data:" + contentType + ";base64," + encoded;
- }
- };